package com.lvyh.lightframe.healthcheck.common;

import com.alibaba.fastjson.JSON;

public class HealthCheckResult<T> {

    private long status; // 0-success ,1-failed
    private String message;
    private T result;

    public static HealthCheckResult SUCCESS() {
        return new HealthCheckResult(ErrorCode.SUCCESS.getStatus(), ErrorCode.SUCCESS.getMessage());
    }

    public static HealthCheckResult FAILED() {
        return new HealthCheckResult(ErrorCode.FAILED.getStatus(), ErrorCode.FAILED.getMessage());
    }

    public static HealthCheckResult create(ErrorCode code, String message) {
        return new HealthCheckResult(code.getStatus(), message);
    }

    public boolean isSuccess() {
        return ErrorCode.SUCCESS.getStatus() == status;
    }

    public HealthCheckResult() {
        super();
    }

    public HealthCheckResult(long status, String message) {
        super();
        this.status = status;
        this.message = message;
    }

    public HealthCheckResult(long status, String message, T result) {
        super();
        this.status = status;
        this.message = message;
        this.result = result;
    }

    public long getStatus() {
        return status;
    }

    public void setStatus(long status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public HealthCheckResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public HealthCheckResult setMsg(String message, Object... args) {
        this.message = args == null || args.length == 0 ? message : String.format(message, args);
        return this;
    }


    public T getResult() {
        return result;
    }

    public HealthCheckResult setResult(T result) {
        this.result = result;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, true);
    }
}
